package core.tool.xml.dom4j.jaxb;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlHelper {
    public static final String TYPE_FILE="0";
    public static final String TYP_STR="1";
	
	private Document document;
	

	public XmlHelper(String configName,String type) {
		
		if(TYPE_FILE.equals(type)) {
			document = createFromFile(configName);
		}else if(TYP_STR.equals(type)) {
			document=createFromString(configName);
		}
	}

	public XmlHelper(Document document) {
		this.document=document;
	}
	
	public static Document createFromFile(String fileName){
		try	{
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(fileName));
			return document;
		}
		catch(DocumentException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static Document createFromString(String sXML) {
		try	{
			Document document=DocumentHelper.parseText(sXML);
			return document;
		}
		catch(DocumentException e){
			return null;
		}
	}	
	
	 
	
	public static Element selectSingleElement(Element e,String sPath){
		if(e.selectNodes(sPath).isEmpty()){
			return null;
		}
		else{
			return (Element) e.selectNodes(sPath).get(0);
		}
	}	
	
	public static Element selectSingleElement(Document e,String sPath){
		if(e.selectNodes(sPath).isEmpty()){
			return null;
		}
		else{
			return (Element) e.selectNodes(sPath).get(0);
		}
	}
	
	public static List<Element> selectSingleElements(Element e,String sPath){
		if(e.selectNodes(sPath).isEmpty()){
			return null;
		}
		else{
			return  e.selectNodes(sPath);
		}
	}
	
	
	public String getConfigValue(String attrXPath) {
		String result="";
		Element element=selectSingleElement(document, attrXPath);
		if(element!=null) {
			result=element.getText();
		}
		return result;
	}

	public String getElementText(Element element) {
		String result=element.getText();
		return result;
	}
	
	
	public String getElementAttribute(String attrXPath,String attribute) {
		Element element=selectSingleElement(document, attrXPath);
		String result=getElementAttribute(element,attribute);
		return result;
	}	
	
	public static String getElementAttribute(Element element,String attribute) {
		String result=element.attributeValue(attribute);
		return result;		
	}
	

	public Element getElementByAttrXpath(String attrXPath) {
		Element element=selectSingleElement(document, attrXPath);
		return element;
	}
	

	public List<Element> getElementsOfXpath(String attrXPath) {
		List<Element> elements=document.selectNodes(attrXPath);
		return elements;
	}
	
	
	public static String getElementPathValue(Element element,String attrXpath) {
		String result="";
		if(element!=null) {
			Element tempElement=selectSingleElement(element, attrXpath);
			if(tempElement!=null) {
				result=tempElement.getText();
			}
		}
		return result;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}
}
